package cn.iocoder.yudao.module.dr.controller.admin.journal;

import cn.iocoder.yudao.module.dr.controller.admin.journal.vo.JournalPageReqVO;
import cn.iocoder.yudao.module.dr.controller.admin.journal.vo.JournalRespVO;
import cn.iocoder.yudao.module.dr.controller.admin.journal.vo.JournalSaveReqVO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.dr.controller.admin.journal.vo.*;
import cn.iocoder.yudao.module.dr.dal.dataobject.journal.JournalDO;
import cn.iocoder.yudao.module.dr.service.journal.JournalService;

@Tag(name = "管理后台 - 期刊信息")
@RestController
@RequestMapping("/dr/journal")
@Validated
public class JournalController {

    @Resource
    private JournalService journalService;

    @PostMapping("/create")
    @Operation(summary = "创建期刊信息")
    @PreAuthorize("@ss.hasPermission('dr:journal:create')")
    public CommonResult<Long> createJournal(@Valid @RequestBody JournalSaveReqVO createReqVO) {
        return success(journalService.createJournal(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新期刊信息")
    @PreAuthorize("@ss.hasPermission('dr:journal:update')")
    public CommonResult<Boolean> updateJournal(@Valid @RequestBody JournalSaveReqVO updateReqVO) {
        journalService.updateJournal(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除期刊信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dr:journal:delete')")
    public CommonResult<Boolean> deleteJournal(@RequestParam("id") Long id) {
        journalService.deleteJournal(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得期刊信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dr:journal:query')")
    public CommonResult<JournalRespVO> getJournal(@RequestParam("id") Long id) {
        JournalDO journal = journalService.getJournal(id);
        return success(BeanUtils.toBean(journal, JournalRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得期刊信息分页")
    @PreAuthorize("@ss.hasPermission('dr:journal:query')")
    public CommonResult<PageResult<JournalRespVO>> getJournalPage(@Valid JournalPageReqVO pageReqVO) {
        PageResult<JournalDO> pageResult = journalService.getJournalPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, JournalRespVO.class));
    }

    /**
     * 资源利用 选择接口查询接口
     * @param pageReqVO
     * @return
     */
    @GetMapping("/selectApply")
    @Operation(summary = "资源利用 选择接口查询接口")
    @PreAuthorize("@ss.hasPermission('dr:journal:query')")
    public CommonResult<PageResult<JournalRespVO>> selectApply(@Valid JournalPageReqVO pageReqVO) {
        PageResult<JournalDO> pageResult = journalService.selectApply(pageReqVO);
        return success(BeanUtils.toBean(pageResult, JournalRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出期刊信息 Excel")
    @PreAuthorize("@ss.hasPermission('dr:journal:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportJournalExcel(@Valid JournalPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<JournalDO> list = journalService.getJournalPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "期刊信息.xls", "数据", JournalRespVO.class,
                        BeanUtils.toBean(list, JournalRespVO.class));
    }

}